﻿<!-- New Post Form -->
<?php 
	include("qlbv_script.php");  ?>
<div id="txtThongBao" style="color:red;"></div>
<div id="postbox">
<form id="new_post" name="new_post" method="post" action="">

<!-- post name -->
<p><label for="title"><h3>Tiêu đề:</h3></label>
<input type="text" id="title" value="" tabindex="1" size="20" name="title" />
</p>

<!-- post Category -->
<p><label for="Category"><h3>Mục:</h3></label>
<p><?php wp_dropdown_categories( 'show_option_none=Category&hide_empty=0&tab_index=4&taxonomy=category' ); ?></p>


<!-- post Content -->
<p><label for="description"><h3>Nội dung:</h3></label>
<textarea id="description" tabindex="3" name="description" cols="70" rows="10"></textarea>
</p>

<!-- post tags -->
<p><label for="post_tags"><h3>Thẻ:</h3></label>
<input type="text" value="" tabindex="5" size="16" name="post_tags" id="post_tags" /></p>
<p>
	<input type="submit" value="<?php if($_GET['action']=='edit' && $_GET['pid']) echo 'Cập nhật'; else echo 'Đăng bài';?>" tabindex="6" id="submit" name="submit" />
	<input type="button" value="Xem trước" tabindex="6" id="preview" name="preview" onclick="xemtruoc();" />
</p>
<input type="hidden" name="action" value="<?php if($_GET['action']=='edit' && $_GET['pid']) echo 'edit'; else echo 'new_post';?>" />
<?php wp_nonce_field( 'new-post' ); ?>
</form>
</div>
<?php 
	if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] )){
		if($_POST['action'] == "new_post" || $_POST['action'] == "edit") {
			// Do some minor form validation to make sure there is content
			if (isset ($_POST['title'])) {
				$title =  $_POST['title'];
			} else {
				echo 'Please enter a  title';
			}
			if (isset ($_POST['description'])) {
				$description = $_POST['description'];
			} else {
				echo 'Please enter the content';
			}
			$tags = $_POST['post_tags'];
			//save the new post
			if($_POST['action'] == "new_post"){
				// Add the content of the form to $post as an array
				$tbao = "Đăng bài";
				$new_post = array(
					'post_title'    => $title,
					'post_content'  => $description,
					'post_category' => array($_POST['cat']),  // Usable for custom taxonomies too
					'tags_input'    => array($tags),
					'post_status'   => 'publish',           // Choose: publish, preview, future, draft, etc.
					'post_type' => 'post'  //'post',page' or use a custom post type if you want to
				);
				$pid = wp_insert_post($new_post); 
			}else{
				// Add the content of the form to $post as an array
				$tbao = "Sửa bài";
				$new_post = array(
					'ID'			=> $_GET['pid'],
					'post_title'    => $title,
					'post_content'  => $description,
					'post_category' => array($_POST['cat']),  // Usable for custom taxonomies too
					'tags_input'    => array($tags),
					'post_status'   => 'publish',           // Choose: publish, preview, future, draft, etc.
					'post_type' => 'post'  //'post',page' or use a custom post type if you want to
				);
				$pid = wp_update_post($new_post); 	 			
			}
			if($pid){
				echo '<script>document.getElementById("txtThongBao").innerText="' . $tbao . ' thành công"</script>';
			}else{
				echo '<script>document.getElementById("txtThongBao").innerText="Lỗi khi ' . $tbao . ', vui lòng thử lại"</script>';
			}
			//insert taxonomies
		}
	}
	if($_GET['action']=='edit' && $_GET['pid']){
		$editPost = get_post($_GET['pid']);
		echo '<br /><div id="previewarea">' . $editPost->post_content . '</div>';
		echo "<script>sua('" . $editPost->post_title . "','" .  
			  implode(",", $editPost->tags_input) . "'," . get_the_category($editPost->ID)[0]->cat_ID . ")</script>";
	}else{	
		echo '<br /><div id="previewarea"></div>';
	}
	//echo apply_filters('the_content',$editPost->post_content);
?>